from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()


class Department(db.Model):
    __tablename__ = 'departments'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, unique=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)

    def to_dict(self):
        # 处理可能为 None 的时间字段
        create_time_str = self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else '未知时间'
        update_time_str = self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else '未知时间'

        return {
            'id': self.id,
            'name': self.name,
            'createTime': create_time_str,
            'updateTime': update_time_str
        }